﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Data;

using StProxyHelper.Entity;
using System.Diagnostics;

namespace StProxyHelper
{
    public class ProxyHelper
    {
        public ProxyHelper()
        { 
        }

        /// <summary>
        /// 初始化数据表
        /// </summary>
        /// <returns></returns>
        private DataTable InitDataTable()
        {
            DataTable dt = new DataTable();

            dt.Columns.Add("Host");
            dt.Columns.Add("Port");
            dt.Columns.Add("Type");
            dt.Columns.Add("SupportQQ", typeof(int));
            dt.Columns.Add("Speed", typeof(int));
            dt.Columns.Add("Area");

            return dt;
        }

        /// <summary>
        /// 从速特网(WWW.SOOOT.CN)获取最新的代理服务器列表
        /// </summary>
        /// <returns>DataTable</returns>
        public DataTable GetProxyListTable()
        {
            string htmlResult = "";
            DataTable dt = InitDataTable();
            HttpHelper httpHelper = new HttpHelper();

            try
            {
                htmlResult = httpHelper.GetHtml("http://www.1flat.com/proxy_list.js");

                string[] strs = htmlResult.Split('=');

                htmlResult = strs[strs.Length - 1 < 0 ? 0 : strs.Length - 1];
                htmlResult = htmlResult.Replace("[[", "[").Replace("]];", "").Replace("[", "").Replace("],", "@").Replace("'", "");
                strs = htmlResult.Split('@');

                for (int i = 0; i < strs.Length; i++)
                {
                    string[] strProxy = strs[i].Split(',');
                    if (strProxy[2] == "2")
                    {
                        strProxy[2] = "匿名";
                    }
                    else if (strProxy[2] == "3")
                    {
                        strProxy[2] = "https";
                    }
                    else
                    {
                        strProxy[2] = "未知";
                    }

                    strProxy[1] = dt.Compute(strProxy[1], "").ToString();

                    dt.Rows.Add(strProxy);
                }
            }
            catch (Exception ex)
            {
                GotoHome();
                throw ex;
            }

            dt.DefaultView.Sort = "Speed";

            return dt.DefaultView.ToTable();
        }

        /// <summary>
        /// 获取一个速度最快的代理
        /// </summary>
        /// <returns></returns>
        public ProxyHost GetTheFastestProxy()
        {
            DataTable dt = GetProxyListTable();
            ProxyHost proxyHost = new ProxyHost();
            StringBuilder sb = new StringBuilder();


            try
            {
                proxyHost.Host = dt.DefaultView.ToTable().Rows[0]["Host"].ToString();
                proxyHost.Port = int.Parse(dt.DefaultView.ToTable().Rows[0]["Port"].ToString());

                sb.Append(proxyHost.Host);
                sb.Append(":");
                sb.Append(proxyHost.Port);

                proxyHost.Address = sb.ToString();
            }
            catch (Exception ex)
            {
                GotoHome();
                throw ex;
            }

            return proxyHost;
        }

        /// <summary>
        /// 测试代理服务器是否可用
        /// </summary>
        /// <param name="ProxyHost"></param>
        /// <returns></returns>
        public bool TestProxy(ProxyHost ProxyHost)
        {
            bool blnResult = false;

            try
            {
                HttpHelper httpHelper = new HttpHelper(ProxyHost);
                string strHtml = httpHelper.GetHtml("http://www.baidu.com");

                if (strHtml.Contains("百度一下"))
                {
                    blnResult = true;
                }
            }
            catch(Exception ex)
            {
                GotoHome();
                throw ex;
            }

            return blnResult;
        }

        /// <summary>
        /// 测试代理服务器是否可用
        /// <para>www.sooot.cn</para>
        /// </summary>
        /// <param name="Address"></param>
        /// <returns></returns>
        public bool TestProxy(string Address)
        {
            bool blnResult = false;

            try
            {
                ProxyHost proxyHost = new ProxyHost();
                proxyHost.Address = Address;
                HttpHelper httpHelper = new HttpHelper(proxyHost);

                string strHtml = httpHelper.GetHtml("http://www.baidu.com");

                if (strHtml.Contains("百度一下"))
                {
                    blnResult = true;
                }
            }
            catch (Exception ex)
            {
                GotoHome();
                throw ex;
            }

            return blnResult;
        }

        private void GotoHome()
        {
            Process.Start("http://www.sooot.cn");
        }
    }
}
